package com.wande.dataplatform.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wande.common.core.domain.R;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.domain.DataModel;
import com.wande.dataplatform.domain.DataModelVersion;
import com.wande.dataplatform.domain.dto.DataModelDTO;
import com.wande.dataplatform.service.IDataModelService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据模型Controller
 *
 * @author ruoyi
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dataplatform/datamodel")
public class DataModelController extends BaseController {

    private final IDataModelService dataModelService;

    /**
     * 查询数据模型列表
     */
    @SaCheckPermission("dataplatform:datamodel:list")
    @GetMapping("/list")
    public TableDataInfo<DataModel> list(DataModel dataModel, PageQuery pageQuery) {
        return dataModelService.queryPageList(dataModel, pageQuery);
    }

    /**
     * 获取数据模型详细信息
     */
    @SaCheckPermission("dataplatform:datamodel:query")
    @GetMapping("/{id}")
    public R<DataModel> getInfo(@PathVariable Long id) {
        return R.ok(dataModelService.queryById(id));
    }

    /**
     * 新增数据模型
     */
    @SaCheckPermission("dataplatform:datamodel:add")
    @Log(title = "数据模型", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Long> add(@RequestBody DataModelDTO dto) {
        Long id = dataModelService.createDataModel(dto);
        return R.ok(id);
    }

    /**
     * 修改数据模型
     */
    @SaCheckPermission("dataplatform:datamodel:edit")
    @Log(title = "数据模型", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@RequestBody DataModelDTO dto) {
        dataModelService.updateDataModel(dto);
        return R.ok();
    }

    /**
     * 删除数据模型
     */
    @SaCheckPermission("dataplatform:datamodel:remove")
    @Log(title = "数据模型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable List<Long> ids) {
        dataModelService.deleteByIds(ids);
        return R.ok();
    }

    /**
     * 验证数据模型
     */
    @SaCheckPermission("dataplatform:datamodel:query")
    @PostMapping("/validate")
    public R<Boolean> validate(@RequestBody DataModelDTO dto) {
        Boolean result = dataModelService.validateModel(dto);
        return R.ok(result);
    }

    /**
     * 同步到Doris
     */
    @SaCheckPermission("dataplatform:datamodel:sync")
    @Log(title = "数据模型", businessType = BusinessType.UPDATE)
    @PostMapping("/{id}/sync")
    public R<Void> syncToDoris(@PathVariable Long id) {
        dataModelService.syncToDoris(id);
        return R.ok();
    }

    /**
     * 查询版本历史
     */
    @SaCheckPermission("dataplatform:datamodel:query")
    @GetMapping("/{id}/versions")
    public R<List<DataModelVersion>> getVersionHistory(@PathVariable Long id) {
        List<DataModelVersion> versions = dataModelService.queryVersionHistory(id);
        return R.ok(versions);
    }

    /**
     * 启用/禁用数据模型
     */
    @SaCheckPermission("dataplatform:datamodel:edit")
    @Log(title = "数据模型", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}/status/{status}")
    public R<Void> updateStatus(@PathVariable Long id, @PathVariable Integer status) {
        dataModelService.updateStatus(id, status);
        return R.ok();
    }

}
